import os

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from utils.spark.sound.rtasr import Client
from ws.settings import BASE_DIR


# Create your views here.
@csrf_exempt
def test_sound(request):
    res = {}


    # gpt = GPT()
    # res = gpt.main(message['text'])[-1]['content']
    method = request.method.upper()

    if method == "GET":
        name = request.GET.get("username", None)
        print("get:" + name)
    #     pwd = request.GET.get("pwd", None)
    #     result["data"] = name
    #     result["pwd"] = pwd
    #     result = json.dumps(result)
        # return HttpResponse(result)
    else:
        sound = request.FILES.get("file", None)
        soundPath = os.path.join(os.path.join(BASE_DIR, 'httpApp/static'),sound.name)

        if sound:
            if not os.path.exists(soundPath):
                destination = open(soundPath,'wb+')
                for chunk in sound.chunks():
                    destination.write(chunk)
                destination.close()

            print("aaa")
            client = Client("41957fc4", "de8b98afd989a0b3797b10d2ca7d5b4b")
            client.send(soundPath)
            # res = client.recv()


    # return HttpResponse(res, content_type="application/json;charset=utf-8")
    return HttpResponse(res)